// category
import { findTopCategory } from '@/api/category'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import { ref } from 'vue'
function useCategory () {
  // 如何在setup中获取路由参数？
  // this.$route.param/query.id
  // useRoute useRoute() -> Route === this.$route
  // 如果我想调用方法 this.$router.push()?
  const route = useRoute()
  const categoryData = ref({})
  async function loadCategory (id = route.params.id) {
    const res = await findTopCategory(id)
    categoryData.value = res.data.result
  }
  loadCategory()
  onBeforeRouteUpdate((to) => { // to参数是指: 新路由对象
    console.log(to.params.id)
    loadCategory(to.params.id)// 重新发送我们的获取新数据的请求
  })
  return {
    categoryData
  }
}

export default useCategory
